package org.itboys.mobile.service;

import org.itboys.admin.entity.lasted.SystemUser;
import org.itboys.admin.service.lasted.SystemUserService;
import org.itboys.admin.tools.AdminSessionHolder;
import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.kitchen.dao.order.KitchenOrderMapper;
import org.itboys.kitchen.entity.mysql.order.KitchenOrder;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mysql.pay.SundayPayConfig;
import org.itboys.mobile.service.mongo.web.SundayMemberService;
import org.itboys.mobile.util.DatetUtil;
import org.itboys.mongodb.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.provider.Sun;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：农厨_后台首页_service
 */
@Service
public class KitchenDesktopService {
    protected Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private KitchenOrderMapper orderMapper;
    @Autowired
    private SundayMemberService memberService;
    @Autowired
    private SystemUserService userService;
    public Map<String,Object> getDesktopDate(){
        Map<String,Object> result = new HashMap<String,Object>();
        Double xsje=0.00;//销售金额
        Double jrxsje=0.00;//今日销售金额
        Integer jrxssl=0;//今日销售订单数

        Double cgje=0.00;//采购总额
        Double jrcgje=0.00;//今日采购总额
        Integer jrcgsl=0;//今日采购订单数量

        Double jfje=0.00;//采购总额
        Double jrjfje=0.00;//今日采购总额
        Integer jrjfsl=0;//今日采购订单数量


        Integer shops=0;//门店数量
        Integer customers=0;//大客户数量
        Integer members =0;//会员数量
        Integer jrmembers=0;//今日会员数
        Map<String,Object> param = new HashMap<String,Object>();
        List<Integer> orderStatuses=new ArrayList<Integer>();
        orderStatuses.add(SundayCommonConstants.ORDER_STATUS_DPS);
        orderStatuses.add(SundayCommonConstants.ORDER_STATUS_PSZ);
        orderStatuses.add(SundayCommonConstants.ORDER_STATUS_YSH);
        orderStatuses.add(SundayCommonConstants.ORDER_STATUS_YPJ);
        param.put("orderStatuses",orderStatuses);

        //2017年7月3日，以登陆后台的门店，过滤对应的订单
        Long userId = AdminSessionHolder.getAdminUserId();
        logger.info("---当前登陆用户ID-首页统计="+userId);
        if(LongUtil.isNotZreo(userId)){
            SystemUser user = userService.getById(userId);
            logger.info("---当前登陆用户ID--首页统计----订单查询"+user.getPlatformMemberId());
            if(user.getPlatformMemberId()!=null){
                param.put("shopId_or_memberId",user.getPlatformMemberId());
            }
        }




        List<KitchenOrder> orders =orderMapper.select(param);
        Long todayStart= DatetUtil.setDateStart(new Date()).getTime();
        Long todayEnd= DatetUtil.setDateEnd(new Date()).getTime();

        for(KitchenOrder order:orders){
            //销售金额
            if(order.getType()==SundayCommonConstants.type_mall){
                xsje+=order.getTotalAmount().doubleValue();
                if(order.getCt().getTime()>todayStart&&order.getCt().getTime()<todayEnd){
                    jrxsje+=order.getTotalAmount().doubleValue();
                    jrxssl++;
                }
            }
            //采购金额
            if(order.getType()==SundayCommonConstants.type_caigou){
                cgje+=order.getTotalAmount().doubleValue();
                if(order.getCt().getTime()>todayStart&&order.getCt().getTime()<todayEnd){
                    jrcgje+=order.getTotalAmount().doubleValue();
                    jrcgsl++;
                }
            }
            //积分订单
            if(order.getType()==SundayCommonConstants.type_score){
                jfje+=order.getTotalAmount().doubleValue();
                if(order.getCt().getTime()>todayStart&&order.getCt().getTime()<todayEnd){
                    jrjfje+=order.getTotalAmount().doubleValue();
                    jrjfsl++;
                }
            }
        }
        //用户信息
        param.clear();
        param.put("isDeleted",0);
        List<SundayMember> sundayMembers=memberService.list(param);
        for(SundayMember sundayMember:sundayMembers){
            if(sundayMember.getUserType()==SundayCommonConstants.user_type_shop){
                shops++;
            }
            if(sundayMember.getUserType()==SundayCommonConstants.user_type_customer){
                customers++;
            }
            if(sundayMember.getUserType()==SundayCommonConstants.user_type_member){
                members++;
                if(sundayMember.getCt()>todayStart&&sundayMember.getCt()<todayEnd){

                    jrmembers++;
                }
            }
        }


        result.put("xsje",new BigDecimal(xsje).setScale(2,BigDecimal.ROUND_HALF_UP));
        result.put("jrxsje",new BigDecimal(jrxsje).setScale(2,BigDecimal.ROUND_HALF_UP));
        result.put("jrxssl",jrxssl);
        result.put("cgje",new BigDecimal(cgje).setScale(2,BigDecimal.ROUND_HALF_UP));
        result.put("jrcgje",new BigDecimal(jrcgje).setScale(2,BigDecimal.ROUND_HALF_UP));
        result.put("jrcgsl",jrcgsl);

        result.put("jfje",new BigDecimal(jfje).setScale(2,BigDecimal.ROUND_HALF_UP));
        result.put("jrjfje",new BigDecimal(jrjfje).setScale(2,BigDecimal.ROUND_HALF_UP));
        result.put("jrjfsl",jrjfsl);



        result.put("shops",shops);
        result.put("customers",customers);
        result.put("members",members);
        result.put("jrmembers",jrmembers);
        return result;
    }

    /**
     * 获取订单提示信息
     * @param request
     * @return
     */
      public Map<String,Object> alert(HttpServletRequest request){
          Map<String,Object> result = new HashMap<String,Object>();
          Map<String,Object> param = new HashMap<String,Object>();
          Integer mallOrders = 0;
          Integer caigouOrders = 0;
          Integer isContinueRequest = 0;
          result.put("mallOrders",mallOrders);//商城订单，根据门店
          result.put("caigouOrders",caigouOrders);//采购订单，根据后台
          result.put("isContinueRequest",isContinueRequest);//控制前端是否继续请求
        //获取登陆用户
          Long userId = AdminSessionHolder.getAdminUserId();
          logger.info("---当前登陆用户ID="+userId);
          if(!LongUtil.isNotZreo(userId))return result;
          SystemUser user = userService.getById(userId);
          logger.info("---当前登陆用户ID--平台用户ID"+user.getPlatformMemberId());
          if(user.getPlatformMemberId()==null)return result;
          //获取待配送的采购订单
          if(user.getPlatformMemberId().longValue()==0l){
              param.clear();
              /*param.put("orderStatus",SundayCommonConstants.ORDER_STATUS_DPS);
              param.put("type",SundayCommonConstants.type_caigou);
              Serializable count = orderMapper.count(param);
              caigouOrders=count==null?0:Integer.valueOf(count.toString());
              isContinueRequest=1;*/
          }else{
           //获取待配送的商城订单
              param.clear();
              param.put("orderStatus",SundayCommonConstants.ORDER_STATUS_DPS);
              param.put("type",SundayCommonConstants.type_mall);
              param.put("shopId",user.getPlatformMemberId());
              //2017年9月16日
              param.put("sendType", SundayCommonConstants.send_type_send);
              Serializable count = orderMapper.count(param);
              mallOrders=count==null?0:Integer.valueOf(count.toString());
              isContinueRequest=1;

          }
          result.put("mallOrders",mallOrders);//商城订单，根据门店
          result.put("caigouOrders",caigouOrders);//采购订单，根据后台
          result.put("isContinueRequest",isContinueRequest);//控制前端是否继续请求
          return result;
      }



}
